package com.suqiu.controller;


import com.suqiu.entity.Order;
import com.suqiu.service.OrderService;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author suqiu
 * @since 2021-12-14
 */
@RefreshScope
@RestController
@RequestMapping("/order")
public class OrderController {

    @Resource
    private OrderService orderService;

    @Resource
    private RedissonClient redissonClient;

    @Resource
//    private UserFeignClient userFeignClient;

    @Value("${spring.datasource.url}")
    private String url;



    @PostMapping("/hehe")
    public String testhehe(@Valid @RequestBody Order order) throws InterruptedException {

//        String suqiu = request.getHeader("Host");
//        System.out.println(suqiu);

        System.out.println("请求进来了" + Thread.currentThread().getName());

        Thread.sleep(5000L);

        RLock lock = redissonClient.getLock("suqiu");
        try {
            if (lock.tryLock()) {
                System.out.println("获取到锁....");
    //            Thread.sleep(2000L);
            }else {
                System.out.println("没获取到锁");
//                Thread.sleep(200L);
            }
        } finally {
            if(lock.isHeldByCurrentThread()){ // 是否是当前执行线程的锁
                lock.unlock(); // 释放锁
            }
        }


//        Order order2 = new Order();
//        order2.setType(1);
//        order2.setStatus(1);
////        int i  = 10 / 0;
//        order2.setItemId(21312313L);
//        order2.setFee(1000);
////        order.setCreateTime(new Date());
////        order.setUpdateTime(new Date());
//        order2.setIsDelete(0);
////		orderMapper.insert(order);
////        orderService.save(order2);

        return  url;

    }

}

